home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / Q-R / RIFF File Format / RIFFcompress.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-09-02  |  15.9 KB  |  854 lines  |  [TEXT/KAHL]

  1. /*
  2. MOD 07-12-87 MAZ - RIFF compression code
  3. */
  4. #include <FileMgr.h>
  5. #include "MAZlib.h"
  6.  
  7. #define assembly 1
  8.  
  9. extern unsigned char *body[];
  10. extern short nrows;
  11. extern short ncols;
  12.  
  13. /* forwards */
  14. bool unhuffscan();
  15. short huffscan();
  16.  
  17. /* readin and decompress a picture */
  18. bool COMPinput(io, huffbuff, chunk)
  19. ioParam *io;
  20. short *huffbuff;
  21. short chunk;
  22.     {
  23.     register short i, j, k, flat;
  24.     register unsigned char *p, *q, *start;
  25.  
  26.     /* determine size of a flat scan line */
  27.     flat = (4 + ncols) & ~1;
  28.     /* read the file scan by scan */
  29.     for (i = 0, start = body[chunk]; i < nrows; i++, start += ncols)
  30.         {
  31.         /* get the scan line header */
  32.         getbytes(io, huffbuff, 4L);
  33.         if (huffbuff[1] > flat || huffbuff[1] < 6)
  34.             {
  35.             /* bad data read */
  36.         bad_data:
  37.             SysBeep(1);
  38.             return(false);
  39.             }
  40.         /* get rest of this scan line */
  41.         getbytes(io, huffbuff+2, (long)(huffbuff[1] - 4));
  42.         /* uncompress the delta's */
  43.         if (!unhuffscan(huffbuff, start, ncols))
  44.             goto bad_data;
  45.         }
  46.     /* now undo the delta calculations in memory */
  47.     /* undo vertical deltas */
  48.     for (i = 1, p = body[chunk], q = p + ncols; i < nrows; i++)
  49.         for (j = 0; j < ncols; j++)
  50.             {
  51.             k = *p;
  52.             p++;
  53.             *q += k;
  54.             q++;
  55.             }
  56.     /* undo horizontal deltas */
  57.     for (i = 0, p = body[chunk]; i < nrows; i++, p++)
  58.         for (j = 0; j < ncols-1; j++)
  59.             {
  60.             k = *p;
  61.             p++;
  62.             *p += k;
  63.             }
  64.     /* success */
  65.     return(true);
  66.     }
  67.  
  68. /* output in compressed format */
  69. /* this procedure coerces the buffer from 256 to "desired_nsamples" samples */
  70. COMPoutput(io, ptr, huffbuff, RLEbuf)
  71. ioParam *io;
  72. unsigned char *ptr;
  73. short *huffbuff;
  74. short *RLEbuf;
  75.     {
  76.     register short i, j, k, flat;
  77.     long outbytes;
  78.     unsigned char *base;
  79.     register unsigned char *p, *q, *start;
  80.  
  81.     /* compute first order (horizontal) deltas */
  82.     for (i = 0, start = ptr; i < nrows; i++, start += ncols)
  83.         for (j = 0, p = start + ncols; j < ncols-1; j++)
  84.             {
  85.             p--;
  86.             k = *(p-1);
  87.             *p -= k;
  88.             }
  89.     /* compute second order (vertical delta of deltas) deltas */
  90.     start = ptr  + (long)ncols * (long)(nrows - 1);
  91.     for (i = 1; i < nrows; i++, start -= ncols)
  92.         for (j = 0, p = start, q = start - ncols; j < ncols; j++)
  93.             {
  94.             k = *q;
  95.             q++;
  96.             *p -= k;
  97.             p++;
  98.             }
  99.     /* note that the first byte of the file is actual data (the anchor) */
  100.     /* the first scanline otherwise consists of first order deltas */
  101.     /* the rest of the scanlines begin with one byte of first order delta */
  102.     /* and follow with second order deltas */
  103.     /* determine size of a flat scan line */
  104.     flat = (4 + ncols) & ~1;
  105.     outbytes = 0;
  106.     for (i = 0, p = ptr; i < nrows; i++, p += ncols)
  107.         {
  108.         j = huffscan(p, ncols, huffbuff);
  109. #if 0
  110.         k = RLEscan(p, ncols, RLEbuf);
  111. #endif
  112.         /* determine if it's smaller unpacked (deltas) */
  113.         if (j > flat)
  114.             {
  115.             j = *p & 0x00FF;
  116.             huffbuff[0] = (1<<8) + j; /* mark flat scan */
  117.             huffbuff[1] = flat;
  118.             /* prezero to pack end with a zero byte */
  119.             huffbuff[flat/2 - 1] = 0;
  120.             cpybuf(&huffbuff[2], p+1, ncols - 1);
  121.             j = flat;
  122.             }
  123.         putbytes(io, huffbuff, (long)j);
  124.         outbytes += j;
  125.         }
  126.     flushbytes(io);
  127.     /* now undo the delta calculations in memory, to restore picture back to normal */
  128.     /* first undo vertical deltas */
  129.     for (i = 1, p = ptr, q = p + ncols; i < nrows; i++)
  130.         for (j = 0; j < ncols; j++)
  131.             {
  132.             k = *p;
  133.             p++;
  134.             *q += k;
  135.             q++;
  136.             }
  137.     /* second undo horizontal deltas */
  138.     for (i = 0, p = ptr; i < nrows; i++, p++)
  139.         for (j = 0; j < ncols-1; j++)
  140.             {
  141.             k = *p;
  142.             p++;
  143.             *p += k;
  144.             }
  145.     }
  146.  
  147. /*
  148.     uncompress a compressed scan line
  149.     return true if scan is OK, false if it would not parse
  150. */
  151. bool unhuffscan(buffer, start, count)
  152. short *buffer;
  153. register unsigned char *start;
  154. short count;
  155.     {
  156.     short flag;
  157.     register short ac, byte;
  158.     register char bitsin;
  159.     register short *q;
  160.     register unsigned char *p;
  161. /* this macro gets the next bit from input - it is left in the sign bit of "ac" */
  162. #define getbit ac += ac; if (--bitsin == 0)  { ac = *q++; bitsin = 16; }
  163.  
  164.     flag = buffer[0]>>8;
  165.     if (flag == 1)
  166.         {
  167.         p = start;
  168.         *p++ = buffer[0] & 0x00FF;
  169.         cpybuf(p,&buffer[2],count-1);
  170.         }
  171.     else if (flag == 0)
  172.         {
  173.         p = start;
  174.         start += count;
  175.         *p++ = buffer[0];
  176.         q = buffer+2;
  177.         ac = *q++;
  178.         bitsin = 16;
  179.         while (p < start)
  180.             {
  181.             /*
  182.                 these nested "if" statements parse the huffman-code
  183.                 tree for RIFF compressed file encoding.
  184.                 at the leaves of the tree are statements like so:
  185.                 
  186.                     *p++ = nnn;
  187.                 
  188.                 these statements write a sample delta byte to the output buffer
  189.                 the parse tree nodes are commented with the bitstrings
  190.                 that they map onto.  an ellipsis in the comment indicates
  191.                 an internal tree node.
  192.             */
  193.             if (ac >= 0)
  194.                 {
  195.                 /* 0 */
  196.                 *p++ = 0;
  197.                 getbit
  198.                 }
  199.             else
  200.                 {
  201.                 /* 1... */
  202.                 getbit
  203.                 if (ac >= 0)
  204.                     {
  205.                     /* 10... */
  206.                     getbit
  207.                     if (ac >= 0)
  208.                         {
  209.                         /* 100 */
  210.                         *p++ = 1;
  211.                         getbit
  212.                         }
  213.                     else
  214.                         {
  215.                         /* 101 */
  216.                         *p++ = 255;
  217.                         getbit
  218.                         }
  219.                     }
  220.                 else
  221.                     {
  222.                     /* 11... */
  223.                     getbit
  224.                     if (ac >= 0)
  225.                         {
  226.                         /* 110... */
  227.                         getbit
  228.                         if (ac >= 0)
  229.                             {
  230.                             /* 1100 */
  231.                             *p++ = 2;
  232.                             getbit
  233.                             }
  234.                         else
  235.                             {
  236.                             /* 1101 */
  237.                             *p++ = 254;
  238.                             getbit
  239.                             }
  240.                         }
  241.                     else
  242.                         {
  243.                         /* 111... */
  244.                         getbit
  245.                         if (ac >= 0)
  246.                             {
  247.                             /* 1110... */
  248.                             getbit
  249.                             if (ac >= 0)
  250.                                 {
  251.                                 /* 11100 */
  252.                                 *p++ = 3;
  253.                                 getbit
  254.                                 }
  255.                             else
  256.                                 {
  257.                                 /* 11101 */
  258.                                 *p++ = 253;
  259.                                 getbit
  260.                                 }
  261.                             }
  262.                         else
  263.                             {
  264.                             /* 1111... */
  265.                             getbit
  266.                             if (ac >= 0)
  267.                                 {
  268.                                 /* 11110... */
  269.                                 getbit
  270.                                 if (ac >= 0)
  271.                                     {
  272.                                     /* 111100 */
  273.                                     *p++ = 4;
  274.                                     getbit
  275.                                     }
  276.                                 else
  277.                                     {
  278.                                     /* 111101 */
  279.                                     *p++ = 252;
  280.                                     getbit
  281.                                     }
  282.                                 }
  283.                             else
  284.                                 {
  285.                                 /* 11111... */
  286.                                 getbit
  287.                                 if (ac >= 0)
  288.                                     {
  289.                                     /* 111110... */
  290.                                     getbit
  291.                                     if (ac >= 0)
  292.                                         {
  293.                                         /* 1111100 */
  294.                                         *p++ = 5;
  295.                                         getbit
  296.                                         }
  297.                                     else
  298.                                         {
  299.                                         /* 1111101 */
  300.                                         *p++ = 251;
  301.                                         getbit
  302.                                         }
  303.                                     }
  304.                                 else
  305.                                     {
  306.                                     /* 111111... */
  307.                                     getbit
  308.                                     if (ac >= 0)
  309.                                         {
  310.                                         /* 1111110... */
  311.                                         getbit
  312.                                         if (ac >= 0)
  313.                                             {
  314.                                             /* 11111100 */
  315.                                             *p++ = 6;
  316.                                             getbit
  317.                                             }
  318.                                         else
  319.                                             {
  320.                                             /* 11111101 */
  321.                                             *p++ = 250;
  322.                                             getbit
  323.                                             }
  324.                                         }
  325.                                     else
  326.                                         {
  327.                                         /* 1111111... */
  328.                                         getbit
  329.                                         if (ac >= 0)
  330.                                             {
  331.                                             /* 11111110... */
  332.                                             getbit
  333.                                             if (ac >= 0)
  334.                                                 {
  335.                                                 /* 111111100 */
  336.                                                 *p++ = 7;
  337.                                                 getbit
  338.                                                 }
  339.                                             else
  340.                                                 {
  341.                                                 /* 111111101 */
  342.                                                 *p++ = 249;
  343.                                                 getbit
  344.                                                 }
  345.                                             }
  346.                                         else
  347.                                             {
  348.                                             /* 11111111... */
  349.                                             /* decode an eight-bit delta which follows */
  350.                                             /* the escape code */
  351.                                             byte = 0;
  352.                                             getbit
  353.                                             if (ac < 0)
  354.                                                 byte++;
  355.                                             byte += byte;
  356.                                             getbit
  357.                                             if (ac < 0)
  358.                                                 byte++;
  359.                                             byte += byte;
  360.                                             getbit
  361.                                             if (ac < 0)
  362.                                                 byte++;
  363.                                             byte += byte;
  364.                                             getbit
  365.                                             if (ac < 0)
  366.                                                 byte++;
  367.                                             byte += byte;
  368.                                             getbit
  369.                                             if (ac < 0)
  370.                                                 byte++;
  371.                                             byte += byte;
  372.                                             getbit
  373.                                             if (ac < 0)
  374.                                                 byte++;
  375.                                             byte += byte;
  376.                                             getbit
  377.                                             if (ac < 0)
  378.                                                 byte++;
  379.                                             byte += byte;
  380.                                             getbit
  381.                                             if (ac < 0)
  382.                                                 byte++;
  383.                                             /* check for bad data */
  384.                                             if (byte < 8 || byte > 248)
  385.                                                 return(false);
  386.                                             *p++ = byte;
  387.                                             getbit
  388.                                             }
  389.                                         }
  390.                                     }
  391.                                 }
  392.                             }
  393.                         }
  394.                     }
  395.                 }
  396.             }
  397.         }
  398.     else if (flag == 2)
  399.         {
  400.         /* input RLE scanline */
  401.         }
  402.     else
  403.         return(false);
  404.     /* success */
  405.     return(true);
  406.     }
  407.  
  408. #if assembly
  409. /* output a compressed scanline definition */
  410. short huffscan(p, count, buffer)
  411. register unsigned char *p;
  412. register short count;
  413. short *buffer;
  414.     {
  415.     register short bitsin, byte, ac;
  416.     register short *q, *disp;
  417.  
  418.     asm
  419.         {
  420.         MOVEA.L    buffer,q
  421.         MOVEQ    #0,byte
  422.         MOVE.B    (p)+,byte
  423.         SUBQ.W    #1,count
  424.         MOVE.W    byte,(q)+            ; write out zero byte (marks compressed scan) and first byte
  425.         MOVE.W    #0,(q)+                ; reserve space for count word
  426.         MOVEQ    #0,ac
  427.         MOVE.B    ac,bitsin
  428. hufflp:    TST.W    count
  429.         BGT.S    @fetch
  430.         TST.B    bitsin                ; write out last part
  431.         BLE.S    @nolast
  432.         SUBI.B    #16,bitsin
  433.         NEG.B    bitsin
  434.         ASL.W    bitsin,ac
  435.         MOVE.W    ac,(q)+
  436. nolast:    goto    endhuff
  437. fetch:    MOVEQ    #0,byte
  438.         MOVE.B    (p)+,byte
  439.         SUBQ.W    #1,count
  440.         CMPI.W    #7,byte
  441.         BLE.S    @disp1
  442.         CMPI.W    #249,byte
  443.         BGE.S    @disp2
  444.         BRA        @defaul
  445. disp1:    LEA        @table,disp            ; load dispatch table addr
  446.         ADD.W    byte,byte            ; get byte times two
  447.         ADD.W    @table(byte),byte    ; get offset from table to code
  448.         JMP        @table(byte)        ; dispatch
  449. table:    DC.W    @case0
  450.         DC.W    @case1
  451.         DC.W    @case2
  452.         DC.W    @case3
  453.         DC.W    @case4
  454.         DC.W    @case5
  455.         DC.W    @case6
  456.         DC.W    @case7
  457. disp2:    LEA        @tabl2,disp            ; load dispatch table addr
  458.         SUBI.W    #249,byte
  459.         ADD.W    byte,byte            ; get byte times two
  460.         ADD.W    @tabl2(byte),byte    ; get offset from tabl2 to code
  461.         JMP        @tabl2(byte)        ; dispatch
  462. tabl2:    DC.W    @cas249
  463.         DC.W    @cas250
  464.         DC.W    @cas251
  465.         DC.W    @cas252
  466.         DC.W    @cas253
  467.         DC.W    @cas254
  468.         DC.W    @cas255
  469. case7:    BSR.S    @onebit
  470. case6:    BSR.S    @onebit
  471. case5:    BSR.S    @onebit
  472. case4:    BSR.S    @onebit
  473. case3:    BSR.S    @onebit
  474. case2:    BSR.S    @onebit
  475. case1:    BSR.S    @onebit
  476.         BSR.S    @zerbit
  477. case0:    BSR.S    @zerbit
  478.         BRA.S    @hufflp
  479. cas249:    BSR.S    @onebit
  480. cas250:    BSR.S    @onebit
  481. cas251:    BSR.S    @onebit
  482. cas252:    BSR.S    @onebit
  483. cas253:    BSR.S    @onebit
  484. cas254:    BSR.S    @onebit
  485. cas255:    BSR.S    @onebit
  486.         BSR.S    @zerbit
  487.         BSR.S    @onebit
  488.         BRA        @hufflp
  489. ; shift a one into our collection register
  490. onebit:    ADD.W    ac,ac
  491.         ADDQ.W    #1,ac
  492.         ADDQ.B    #1,bitsin
  493.         CMPI.B    #16,bitsin
  494.         BNE.S    @oneret
  495.         MOVE.W    ac,(q)+
  496.         MOVEQ    #0,ac
  497.         MOVE.B    ac,bitsin
  498. oneret:    RTS
  499. ; shift a zero into our collection register
  500. zerbit:    ADD.W    ac,ac
  501.         ADDQ.B    #1,bitsin
  502.         CMPI.B    #16,bitsin
  503.         BNE.S    @zerret
  504.         MOVE.W    ac,(q)+
  505.         MOVEQ    #0,ac
  506.         MOVE.B    ac,bitsin
  507. zerret:    RTS
  508. ; shift in 8 bits of ones followed by the 8-bit (delta) code
  509. defaul:    BSR.S    @onebit
  510.         BSR.S    @onebit
  511.         BSR.S    @onebit
  512.         BSR.S    @onebit
  513.         BSR.S    @onebit
  514.         BSR.S    @onebit
  515.         BSR.S    @onebit
  516.         BSR.S    @onebit
  517. ; now shift in the 8-bit delta code itself.
  518.         ASL.B    #1,byte
  519.         ADDX.W    ac,ac            ; shift in a bit
  520.         BSR.S    @putbit
  521.         ASL.B    #1,byte
  522.         ADDX.W    ac,ac            ; shift in a bit
  523.         BSR.S    @putbit
  524.         ASL.B    #1,byte
  525.         ADDX.W    ac,ac            ; shift in a bit
  526.         BSR.S    @putbit
  527.         ASL.B    #1,byte
  528.         ADDX.W    ac,ac            ; shift in a bit
  529.         BSR.S    @putbit
  530.         ASL.B    #1,byte
  531.         ADDX.W    ac,ac            ; shift in a bit
  532.         BSR.S    @putbit
  533.         ASL.B    #1,byte
  534.         ADDX.W    ac,ac            ; shift in a bit
  535.         BSR.S    @putbit
  536.         ASL.B    #1,byte
  537.         ADDX.W    ac,ac            ; shift in a bit
  538.         BSR.S    @putbit
  539.         ASL.B    #1,byte
  540.         ADDX.W    ac,ac            ; shift in a bit
  541.         BSR.S    @putbit
  542.         BRA        @hufflp
  543. ; a little subroutine to shift our bit collection register.
  544. putbit:    ADDQ.B    #1,bitsin
  545.         CMPI.B    #16,bitsin
  546.         BNE.S    @bitret
  547.         MOVE.W    ac,(q)+
  548.         MOVEQ    #0,ac
  549.         MOVE.B    ac,bitsin
  550. bitret:    RTS
  551.         }
  552. endhuff:
  553.     /* write out count word */
  554.     ac = (q - buffer) * 2;
  555.     buffer[1] = ac;
  556.     return(ac);
  557.     }
  558. #else
  559. /* output a compressed scanline definition */
  560. short huffscan(p, count, buffer)
  561. register unsigned char *p;
  562. register short count;
  563. short *buffer;
  564.     {
  565.     register short bitsin, byte, ac;
  566.     register short *q;
  567.  
  568.     q = buffer;
  569.     /* put out first byte by itself at beginning (as a word) */
  570.     *q++ = *p++;
  571.     count--;
  572.     q++; /* skip count word */
  573.     /* set up bit output buffer - a short */
  574.     ac = 0;
  575.     bitsin = 0;
  576.     while (count > 0)
  577.         {
  578.         /* fetch data byte */
  579.         byte = *p;
  580.         p++;
  581.         count--;
  582.         /* output it in bits */
  583.         switch (byte)
  584.             {
  585.         case 7:
  586.             ac += ac + 1; /* shift in a one */
  587.             bitsin++;
  588.             if (bitsin == 16)
  589.                 {
  590.                 *q++ = ac;
  591.                 bitsin = ac = 0;
  592.                 }
  593.         case 6:
  594.             ac += ac + 1; /* shift in a one */
  595.             bitsin++;
  596.             if (bitsin == 16)
  597.                 {
  598.                 *q++ = ac;
  599.                 bitsin = ac = 0;
  600.                 }
  601.         case 5:
  602.             ac += ac + 1; /* shift in a one */
  603.             bitsin++;
  604.             if (bitsin == 16)
  605.                 {
  606.                 *q++ = ac;
  607.                 bitsin = ac = 0;
  608.                 }
  609.         case 4:
  610.             ac += ac + 1; /* shift in a one */
  611.             bitsin++;
  612.             if (bitsin == 16)
  613.                 {
  614.                 *q++ = ac;
  615.                 bitsin = ac = 0;
  616.                 }
  617.         case 3:
  618.             ac += ac + 1; /* shift in a one */
  619.             bitsin++;
  620.             if (bitsin == 16)
  621.                 {
  622.                 *q++ = ac;
  623.                 bitsin = ac = 0;
  624.                 }
  625.         case 2:
  626.             ac += ac + 1; /* shift in a one */
  627.             bitsin++;
  628.             if (bitsin == 16)
  629.                 {
  630.                 *q++ = ac;
  631.                 bitsin = ac = 0;
  632.                 }
  633.         case 1:
  634.             ac += ac + 1; /* shift in a one */
  635.             bitsin++;
  636.             if (bitsin == 16)
  637.                 {
  638.                 *q++ = ac;
  639.                 bitsin = ac = 0;
  640.                 }
  641.             ac += ac; /* shift in a zero */
  642.             bitsin++;
  643.             if (bitsin == 16)
  644.                 {
  645.                 *q++ = ac;
  646.                 bitsin = ac = 0;
  647.                 }
  648.         case 0:
  649.             ac += ac; /* shift in a zero */
  650.             bitsin++;
  651.             if (bitsin == 16)
  652.                 {
  653.                 *q++ = ac;
  654.                 bitsin = ac = 0;
  655.                 }
  656.             break;
  657.         case 249:
  658.             ac += ac + 1; /* shift in a one */
  659.             bitsin++;
  660.             if (bitsin == 16)
  661.                 {
  662.                 *q++ = ac;
  663.                 bitsin = ac = 0;
  664.                 }
  665.         case 250:
  666.             ac += ac + 1; /* shift in a one */
  667.             bitsin++;
  668.             if (bitsin == 16)
  669.                 {
  670.                 *q++ = ac;
  671.                 bitsin = ac = 0;
  672.                 }
  673.         case 251:
  674.             ac += ac + 1; /* shift in a one */
  675.             bitsin++;
  676.             if (bitsin == 16)
  677.                 {
  678.                 *q++ = ac;
  679.                 bitsin = ac = 0;
  680.                 }
  681.         case 252:
  682.             ac += ac + 1; /* shift in a one */
  683.             bitsin++;
  684.             if (bitsin == 16)
  685.                 {
  686.                 *q++ = ac;
  687.                 bitsin = ac = 0;
  688.                 }
  689.         case 253:
  690.             ac += ac + 1; /* shift in a one */
  691.             bitsin++;
  692.             if (bitsin == 16)
  693.                 {
  694.                 *q++ = ac;
  695.                 bitsin = ac = 0;
  696.                 }
  697.         case 254:
  698.             ac += ac + 1; /* shift in a one */
  699.             bitsin++;
  700.             if (bitsin == 16)
  701.                 {
  702.                 *q++ = ac;
  703.                 bitsin = ac = 0;
  704.                 }
  705.         case 255:
  706.             ac += ac + 1; /* shift in a one */
  707.             bitsin++;
  708.             if (bitsin == 16)
  709.                 {
  710.                 *q++ = ac;
  711.                 bitsin = ac = 0;
  712.                 }
  713.             ac += ac; /* shift in a zero */
  714.             bitsin++;
  715.             if (bitsin == 16)
  716.                 {
  717.                 *q++ = ac;
  718.                 bitsin = ac = 0;
  719.                 }
  720.             ac += ac + 1; /* shift in a one */
  721.             bitsin++;
  722.             if (bitsin == 16)
  723.                 {
  724.                 *q++ = ac;
  725.                 bitsin = ac = 0;
  726.                 }
  727.             break;
  728.         default:
  729.             ac += ac + 1; /* shift in a one */
  730.             bitsin++;
  731.             if (bitsin == 16)
  732.                 {
  733.                 *q++ = ac;
  734.                 bitsin = ac = 0;
  735.                 }
  736.             ac += ac + 1; /* shift in a one */
  737.             bitsin++;
  738.             if (bitsin == 16)
  739.                 {
  740.                 *q++ = ac;
  741.                 bitsin = ac = 0;
  742.                 }
  743.             ac += ac + 1; /* shift in a one */
  744.             bitsin++;
  745.             if (bitsin == 16)
  746.                 {
  747.                 *q++ = ac;
  748.                 bitsin = ac = 0;
  749.                 }
  750.             ac += ac + 1; /* shift in a one */
  751.             bitsin++;
  752.             if (bitsin == 16)
  753.                 {
  754.                 *q++ = ac;
  755.                 bitsin = ac = 0;
  756.                 }
  757.             ac += ac + 1; /* shift in a one */
  758.             bitsin++;
  759.             if (bitsin == 16)
  760.                 {
  761.                 *q++ = ac;
  762.                 bitsin = ac = 0;
  763.                 }
  764.             ac += ac + 1; /* shift in a one */
  765.             bitsin++;
  766.             if (bitsin == 16)
  767.                 {
  768.                 *q++ = ac;
  769.                 bitsin = ac = 0;
  770.                 }
  771.             ac += ac + 1; /* shift in a one */
  772.             bitsin++;
  773.             if (bitsin == 16)
  774.                 {
  775.                 *q++ = ac;
  776.                 bitsin = ac = 0;
  777.                 }
  778.             ac += ac + 1; /* shift in a one */
  779.             bitsin++;
  780.             if (bitsin == 16)
  781.                 {
  782.                 *q++ = ac;
  783.                 bitsin = ac = 0;
  784.                 }
  785.             /* now shift in the 8-bit code */
  786.             ac += ac + ((byte>>7)&1);
  787.             bitsin++;
  788.             if (bitsin == 16)
  789.                 {
  790.                 *q++ = ac;
  791.                 bitsin = ac = 0;
  792.                 }
  793.             ac += ac + ((byte>>6)&1);
  794.             bitsin++;
  795.             if (bitsin == 16)
  796.                 {
  797.                 *q++ = ac;
  798.                 bitsin = ac = 0;
  799.                 }
  800.             ac += ac + ((byte>>5)&1);
  801.             bitsin++;
  802.             if (bitsin == 16)
  803.                 {
  804.                 *q++ = ac;
  805.                 bitsin = ac = 0;
  806.                 }
  807.             ac += ac + ((byte>>4)&1);
  808.             bitsin++;
  809.             if (bitsin == 16)
  810.                 {
  811.                 *q++ = ac;
  812.                 bitsin = ac = 0;
  813.                 }
  814.             ac += ac + ((byte>>3)&1);
  815.             bitsin++;
  816.             if (bitsin == 16)
  817.                 {
  818.                 *q++ = ac;
  819.                 bitsin = ac = 0;
  820.                 }
  821.             ac += ac + ((byte>>2)&1);
  822.             bitsin++;
  823.             if (bitsin == 16)
  824.                 {
  825.                 *q++ = ac;
  826.                 bitsin = ac = 0;
  827.                 }
  828.             ac += ac + ((byte>>1)&1);
  829.             bitsin++;
  830.             if (bitsin == 16)
  831.                 {
  832.                 *q++ = ac;
  833.                 bitsin = ac = 0;
  834.                 }
  835.             ac += ac + (byte&1);
  836.             bitsin++;
  837.             if (bitsin == 16)
  838.                 {
  839.                 *q++ = ac;
  840.                 bitsin = ac = 0;
  841.                 }
  842.             break;
  843.             }
  844.         }
  845.     /* now write out last part */
  846.     if (bitsin > 0)
  847.         *q++ = ac << (16 - bitsin);
  848.     /* write out count word */
  849.     ac = (q - buffer) * 2;
  850.     buffer[1] = ac;
  851.     return(ac);
  852.     }
  853. #endif
  854.